﻿using UnityEngine;
using System.Collections.Generic;
using System.Collections;
public class CandyEffect : MonoBehaviour
{
    public static CandyEffect instance;
    // Use this for initialization
    public List<GameObject> candy_electric;
    void Start()
    {
        electricEffectList = new List<GameObject>();
        candy_electric = new List<GameObject>();
    }
    void Awake()
    {
        instance = this;
    }
    internal void CreateEffect(GameObject obj_type, string type)
    {
		if(obj_type==null){return;}
        if (type == "horizontal")
        {
            GameObject temp1 = (GameObject)Instantiate(GamePrefabs.instance.horWavePrefab, obj_type.transform.position, Quaternion.identity);
            temp1.transform.localEulerAngles = new Vector3(90, 0, 0);
            iTween.MoveBy(temp1, new Vector3(10, 0, 0), 1.5f);
			Destroy(temp1, 2f);

            GameObject temp2 = (GameObject)Instantiate(GamePrefabs.instance.horWavePrefab, obj_type.transform.position, Quaternion.identity);
			temp2.transform.localEulerAngles = new Vector3(90, 0, 0);
            iTween.MoveBy(temp2, new Vector3(-10, 0, 0), 1.5f);
			Destroy(temp2, 2f);
        }else if (type == "vertical")
		{
            GameObject temp3 = (GameObject)Instantiate(GamePrefabs.instance.verWavePrefab, obj_type.transform.position, Quaternion.identity);
            temp3.transform.localEulerAngles = new Vector3(90, 0, 0);
            iTween.MoveBy(temp3, new Vector3(0, 20, 0), 1.5f);
			Destroy(temp3, 2f);

            GameObject temp4 = (GameObject)Instantiate(GamePrefabs.instance.verWavePrefab, obj_type.transform.position, Quaternion.identity);
            temp4.transform.localEulerAngles = new Vector3(90, 0, 0);
            iTween.MoveBy(temp4, new Vector3(0, -10, 0), 1.5f);
			Destroy(temp4, 2f);
        }
		GameManager.getInstance().gamestate=GameState.destroy;
		GameManager.getInstance().IsDestroySpecialMatch=false;
		CandyDrop.instance.Invoke("DroppingCandy", 0.1f);
    }
    private float delay = 0.09f;
    private Vector3 point1;
    private Vector3 starPos;
    private List<GameObject> electricEffectList;
    internal void AssignBurstUniversalObject(Vector3 starPos, bool is_vertical_horizontal)
    {
		if(GameManager.getInstance().gamestate!=GameState.destroy){return;}
		GameManager.getInstance().IsDestroySpecialMatch=true;
		GameManager.getInstance().gamestate=GameState.destroy;
        this.starPos = starPos;
        StartCoroutine(InstanciateElectricEffect(is_vertical_horizontal));

    }
    GameObject temp;
	Vector3 pos ;
    private string[] special_type = { "horizontal", "vertical" };
    private IEnumerator InstanciateElectricEffect(bool is_vertical_horizontal)
    {

        for (int i = 0; i < candy_electric.Count; i++)
        {
            point1 = candy_electric[i].transform.position;
			if(point1.y>CandyDrop.instance.bar.transform.position.y){continue;}
          pos  = (point1 + starPos) * .4f + new Vector3(0, 0, -2);
            if (is_vertical_horizontal)
            {
                Match.instance.ChangeType(candy_electric[i], special_type[Random.Range(0, special_type.Length)]);
            }
            temp = (GameObject)Instantiate(GamePrefabs.instance.electricFieldPrefab, pos, Quaternion.identity);
            temp.transform.localScale = new Vector3(Vector2.Distance(point1, starPos) - 1f, temp.transform.localScale.y, temp.transform.localScale.z);
            temp.transform.rotation = Quaternion.LookRotation(point1 - starPos);

            float angleX = temp.transform.localEulerAngles.x;
            if (starPos.x < point1.x)
                angleX = 180 - angleX;

            temp.transform.localEulerAngles = new Vector3(0, 0, angleX);
            electricEffectList.Add(temp);
            yield return new WaitForSeconds(delay);
        }

        Invoke("DestroyEffect", (delay * candy_electric.Count) + 0.05f);
        if (is_vertical_horizontal)
        {
            Invoke("MyDestroy", 0.6f);

        }
        else
        {
			GameManager.getInstance().IsDestroySpecialMatch=false;
			GameManager.getInstance().gamestate=GameState.destroy;
            CandyDrop.instance.Invoke("DroppingCandy", (delay * candy_electric.Count) + 0.05f);
            candy_electric.Clear();
        }
    }
    private void MyDestroy()
    {
        StartCoroutine(DestroyCandy());
    }
    private IEnumerator DestroyCandy()
    {
        for (int i = 0; i < candy_electric.Count; i++)
        {
			candy_electric[i].GetComponent<CandyController>().IsMatched = true;
			CandyDrop.instance.DroppingCandy();
            yield return new WaitForSeconds(1.7f);
        }
		GameManager.getInstance().IsDestroySpecialMatch=false;
		GameManager.getInstance().gamestate=GameState.destroy;
		CandyDrop.instance.DroppingCandy();
        candy_electric.Clear();
    }
    internal void DestroyEffect()
    {
        for (int i = 0; i < electricEffectList.Count; i++)
        {
            Destroy(electricEffectList[i]);
        }
    }


}
